use "actions T1 - T6.dta", clear
drop message
save action, replace

use "chat rich communication.dta", clear

drop if Period==.

gen KAK=1 if KAKat<6
gen CHK=1 if CHKat<6

replace KAK=6 if KAKat>5&KAKat<9
replace CHK=6 if CHKat>5&CHKat<9

gen type=0
replace type=KAK if KAK==CHK

drop if type>8|type==0


rename SubjectID Subject

sort session Period Subject

gen message=3 if type<6 
replace message=2 if type>5&type<9


keep session Period Subject  seq TimeStamp message
gen communication="Rich"

save chatR, replace

use "messages bare communication", clear

gen message=3 if Message=="C"
replace message=2 if Message=="B"
replace message=2 if Message=="A"


keep session Period Subject  message seq TimeStamp

gen communication="Bare"
save chatB, replace


append using ChatR
save Chat, replace

sort session Period Subject

joinby session Period Subject using "action", unmatched(both)

replace message=0 if message==.

sort session MatchingGroup NetworkGroup
egen NetGroup=group(session MatchingGroup NetworkGroup)


sort NetGroup Period seq
by NetGroup Period: gen seq2=_n


local i=1
 while `i'<33 {
 
 gen mess`i'=message if Subject==`i'&treatment>2
 
 local i=`i'+1
 }

 
 local i=1
while `i'<33 {
sort NetGroup Period seq2

by NetGroup Period: replace mess`i'=mess`i'[_n-1] if mess`i'==.&_n>1&treatment>2

local i=`i'+1
}



local i=1
while `i'<33 {

sort NetGroup Period seq2
by NetGroup Period: replace mess`i'=0 if mess`i'==.&treatment>2

local i=`i'+1
}


local j=1
while `j'<5 {
local i=1
while `i'<33 {

replace MessagesReceived`j'=mess`i' if   LinkedSubjects`j'==`i'&treatment>2

local i=`i'+1
 }
local j=`j'+1
 }

 sort NetGroup Period Subject seq2
 
 by NetGroup Period Subject: keep if _n==_N

 
replace message=message==3 

 
local j=1
while `j'<5 {

replace MessagesReceived`j'=MessagesReceived`j'==3&treatment>2

 
local j=`j'+1
 }

gen messCrec2=MessagesReceived1+MessagesReceived2+MessagesReceived3+MessagesReceived4 
 ***************************************
 ************************************
 
 
 local i=1
 while `i'<33 {
 drop mess`i'
 gen mess`i'=message if Subject==`i'&treatment>2
 
 local i=`i'+1
 }

 
 local i=1
while `i'<33 {
sort NetGroup Period

by NetGroup Period: egen _mess`i'=total(mess`i')

local i=`i'+1
}



local j=1
while `j'<5 {
gen MessR`j'=0
local i=1
while `i'<33 {

replace MessR`j'=_mess`i' if   LinkedSubjects`j'==`i'&treatment>2

local i=`i'+1
 }
local j=`j'+1
 }

 
 gen messCrec=MessR1+MessR2+MessR3+MessR4
 
 
 **************************************
 **************************************
 
 
 
gen network=treatment==2|treatment==4|treatment==6
gen open=treatment>4

sort session Subject Period
gen OwnA_1=OwnAction[_n-1]
gen Oth_1=OtherAction[_n-1]
gen badexp=OwnA_1==3&Oth_1<3



sort session MatchingGroup
egen idgroup=group(session MatchingGroup)


sort treatment session MatchingGroup
egen ggg= group(treatment session MatchingGroup)

sort treatment session Subject Period
by treatment session Subject: gen act3=OwnAction[_n-1]==3

sort session Subject
egen id_ind=group(session Subject)
xtset id_ind Period

gen OwnC=OwnAction==3


***credibility

gen pippo=OwnAction==3
gen topo=(pippo==1&message==1)

sort ggg Period message 
by ggg Period message: egen P=mean(topo) if Period==1&message==1

sort ggg Period  
by ggg Period: egen PP=mean(P) if Period==1

drop P

gen messs=message
replace messs=. if message==0


local i=1
while `i'<33 {
replace mess`i'=.
replace mess`i'=messs if Subject==`i'
local i=`i'+1
}


local i=1
while `i'<33 {
sort session Period
by session Period: egen m`i'=mean(mess`i')

replace mess`i'=m`i' 
drop m`i'
local i=`i'+1
}

gen message_other=.


local i=1
while `i'<33{
replace message_other=mess`i' if LinkedToSubject==`i'
local i=`i'+1
}

replace message_other=0 if message_other==.


*********************


**drop if OtherAction==0

gen II=0
replace II= 1 if OtherAction==3&message_other==1
local gamma=0
local dgamma=0
sort ggg Subject Period
by ggg Subject: gen III=II[_n-1]
replace II=III
drop III

*** here we build variables Credibility for different values of gamma

while `gamma'<1.01 {

gen PP_`dgamma'=PP if Period==1

local t=2

while `t'<41{
sort session Subject Period
by session Subject: replace PP_`dgamma'=PP_`dgamma'[_n-1] *`gamma'+ (1-`gamma')*II if Period==`t'&message_other[_n-1]==1
by session Subject: replace PP_`dgamma'=PP_`dgamma'[_n-1] if Period==`t'&message_other[_n-1]==0
by session Subject: replace PP_`dgamma'=PP_`dgamma'[_n-1] if Period==`t'&OtherAction[_n-1]==0
local t=`t'+1
}

local dgamma=`dgamma'+1
local gamma=`gamma'+0.1
}



  sort ggg Subject Period
  by ggg Subject: gen mess_1=message[_n-1]
  
  *** Table 6 
  
  tab  messCrec2 treatment if treatment>4, sum(message)
  
  
  *** Table D4
  
  tab  messCrec2 treatment if treatment>2&treatment<5, sum(message)
  
  
  
  *****  Table B3
  
*** As described in the paper the best value of gamme is 0.9. Hence we use variable PP_9 (Credibility computed by gamma=0.9)  
  
 heckprobit OwnC  c.Period#i.open  i.network#i.open c.messCrec PP_9 if treatment>2, select(message = c.Period#i.open i.mess_1 c.messCrec2 i.open#i.network  ) vce(cluster ggg)
  

    *****  Table 9
  
  
   margin, dydx(network messCrec PP_9 Period) at(open=(0 1)) predict(pcond) 
   margin, dydx(network messCrec2 Period mess_1) at(open=(0 1)) predict(psel) 
   margin, dydx(network messCrec Period mess_1 PP_9) at(open=(0 1)) predict(pmargin) 
   margin, dydx(open) at(network=(0 1)) predict(pcond) 
   margin, dydx(open) at(network=(0 1)) predict(psel) 
   margin, dydx(open) at(network=(0 1)) predict(pmargin) 
  
  
    predict prediction, pcond
  
 **** Figure 5 
  sort open network Period message
by open network Period message:egen pred=mean(prediction)
  sort open network Period message
by open network Period message:egen obs=mean(OwnC)

scatter pred obs Period if message==1, by(open network)


*****  Figure 4 (only data, for the figure use column cum )


sort session MatchingGroup NetworkGroup Period
by session MatchingGroup  NetworkGroup Period: egen cum=total(message)

preserve
by session MatchingGroup  NetworkGroup Period: keep if _n==1
sort treatment
by treatment: tab cum
restore


